﻿using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Windows.Data;
using Microsoft.Practices.Prism.Commands;
using ParaEditor.Common;
using ParaEditor.DAO;
using ParaEditor.DAO.ParaDAO;
using ParaEditor.Desktop.Interfaces;
using ParaEditor.Domain.Entities;
using ParaEditor.Infrastructure;
using System.Globalization;

namespace ParaEditor.Desktop.ViewModel
{
    [Export(typeof(IParaVersionViewModel))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class ParaVersionViewModel : ViewModelBase, IParaVersionViewModel
    {
        
        
        private GlobelValue globelValue = null;
        private VersionContrastDAO paramDao;
        private VersionCtrlDAO versionCtrlDao;
        private InnerVerCtrlDAO innerVerCtrlDAO;
        private string[] paramsID = { "3006", "3007","3011","3082","3083","3084","3085","4001","4002","4003","4004","4005","4006","4009","4010","4011","4015","4030"};
        private ICollectionView paraCollectionDetail;
        DateTimeFormatInfo dtFormat = new DateTimeFormatInfo();
        
        public ICollectionView ParaCollectionDetail
        {
            get
            {
                return this.paraCollectionDetail;
            }
            set
            {
                this.paraCollectionDetail = value;
                this.RaisePropertyChangedEvent("ParaCollectionDetail");
            }
        }

        private short versionNum;
        public short VersionNum
        {
            get
            {
                return this.versionNum;
            }
            set
            {
                this.versionNum = value;
                this.RaisePropertyChangedEvent("VersionNum");
            }
        }

        private string paramInfo;
        public string ParamInfo
        {
            get
            {
                return this.paramInfo;
            }
            set
            {
                this.paramInfo = value;
                this.RaisePropertyChangedEvent("ParamInfo");
            }
        }

        private DateTime startTime;
        public DateTime StartTime
        {
            get
            {
                return this.startTime;
            }
            set
            {
                this.startTime = value;
                this.RaisePropertyChangedEvent("StartTime");
            }
        }

        private DateTime endTime;
        public DateTime EndTime
        {
            get
            {
                return this.endTime;
            }
            set
            {
                this.endTime = value;
                this.RaisePropertyChangedEvent("EndTime");
            }
        }

        public DelegateCommand UpVersionCommand { get; private set; }
        public DelegateCommand ShengXiaoCommand { get; private set; }
        public DelegateCommand SaveTimeCommand { get; private set; }
        private Hashtable ParamTypeTable;
        private Hashtable ParamDetailTable;


        [ImportingConstructor]
        public ParaVersionViewModel()
        {
            dtFormat.FullDateTimePattern = "yyyyMMddHHmmss";
            innerVerCtrlDAO = new InnerVerCtrlDAO();
            versionCtrlDao = new VersionCtrlDAO();
            globelValue = new GlobelValue();
            paramDao = new VersionContrastDAO();
            ParamDetailTable = paramDao.GetAllParamTypeDetail();
            ObservableCollection<VersionContrast> versionConetastCollection = new ObservableCollection<VersionContrast>();
            versionConetastCollection = paramDao.GetVersionContrast();
            ObservableCollection<ParamDetail> paramDetailCollection = Convert2ParamDetail(versionConetastCollection);
            ParaCollectionDetail = CollectionViewSource.GetDefaultView(paramDetailCollection);
            if (versionConetastCollection.Count > 0)
            {
                this.VersionNum = versionConetastCollection[0].versionContrastPk.versionNum;
            }

            if (ParaCollectionDetail != null)
            {
                ParaCollectionDetail.CurrentChanged += ParaCollectionDetail_CurrentChanged;
            }

            ParamTypeTable = paramDao.GetAllParamType();

            //Button
            this.UpVersionCommand = new DelegateCommand(this.UpVersion);
            this.ShengXiaoCommand = new DelegateCommand(this.ShengXiao);
            this.SaveTimeCommand = new DelegateCommand(this.SaveTime);
        }

        private void SaveTime()
        {
            if (ParaCollectionDetail.CurrentItem != null)
            {
                InnerVerCtrl innerVerCtrl = new InnerVerCtrl();
                InnerVerCtrlPk innerVerCtrlPk = new InnerVerCtrlPk();
                ParamDetail paramDetail = ParaCollectionDetail.CurrentItem as ParamDetail;
                innerVerCtrlPk.versionNum = paramDetail.ParameterNum;
                innerVerCtrlPk.versionOwner = paramDetail.ParameterID;
                innerVerCtrl.innerVerCtrlPk = innerVerCtrlPk;
                innerVerCtrl.verifySt = "1";
                innerVerCtrl.versionSt = "1";
                innerVerCtrl.canIssue = "1";

                if (this.StartTime != null)
                {
                    innerVerCtrl.startTime = this.StartTime.ToString("yyyyMMddHHmmss");
                }
                else
                {
                    Microsoft.Windows.Controls.MessageBox.Show("请填写启用时间!");
                    return;
                }

                if (this.EndTime != null)
                {
                    innerVerCtrl.endTime = this.EndTime.ToString("yyyyMMddHHmmss");
                }
                else
                {
                    Microsoft.Windows.Controls.MessageBox.Show("请填写结束时间!");
                    return;
                }
                ResultValue rv = innerVerCtrlDAO.CreateObject(innerVerCtrl);
                if (rv.Code != 0)
                {
                    Microsoft.Windows.Controls.MessageBox.Show("保存失败！发生错误:" + rv.Msg);
                }
                else
                {
                    Microsoft.Windows.Controls.MessageBox.Show("保存成功！");
                }
            }
            else
            {
                Microsoft.Windows.Controls.MessageBox.Show("请选择要为其设置时间的参数");
            }
        }

        private void ShengXiao()
        {
            VersionCtrl versionCtrl = null;
            VersionCtrlPk versionCtrlPk = null;
            

            VersionContrast vc = null;
            VersionContrastPk vcPk = null;
            //查询当前所有参数的最大info_ver
            foreach (string item in paramsID)
            {
                vcPk = new VersionContrastPk();
                vc = new VersionContrast();
                vcPk.versionNum = this.VersionNum;
                vcPk.parameterId = (string)ParamTypeTable[item];
                vc.versionContrastPk = vcPk;
                vc.parameterNum = Int16.Parse(paramDao.GetMaxID(item).ToString());
                ResultValue result = paramDao.CreateObject(vc);
                if (result.Code != 0)
                {
                    Microsoft.Windows.Controls.MessageBox.Show("保存失败！发生错误:" + result.Msg);
                }

                versionCtrlPk = new VersionCtrlPk();
                versionCtrl = new VersionCtrl();
                versionCtrlPk.versionNum = this.VersionNum;
                versionCtrlPk.versionContent = (string)ParamTypeTable[item];
                versionCtrl.versionCtrlPk = versionCtrlPk;
                versionCtrl.verifySt = "1";
                versionCtrl.versionCrtTime = DateTime.Now.ToString("yyyyMMddHHmmss");//生成时间
                versionCtrl.verifyTime = DateTime.Now.ToString("yyyyMMddHHmmss");//确认时间
                versionCtrl.startTime = DateTime.Now.ToString("yyyyMMddHHmmss");//启用时间

                ResultValue result1 = versionCtrlDao.CreateObject(versionCtrl);
                if (result1.Code != 0)
                {
                    Microsoft.Windows.Controls.MessageBox.Show("保存失败！发生错误:" + result1.Msg);
                }
            }
            Microsoft.Windows.Controls.MessageBox.Show("生效完成");
            ReflashDataGrid();
        }

        private void UpVersion()
        {
            this.VersionNum += 1;
        }

        private void ReflashDataGrid()
        {
            //paramDao.GetVersionContrast()
            ObservableCollection<ParamDetail> paramDetailCollection = Convert2ParamDetail(paramDao.GetVersionContrast());
            ParaCollectionDetail = CollectionViewSource.GetDefaultView(paramDetailCollection);
            ParaCollectionDetail.CurrentChanged += ParaCollectionDetail_CurrentChanged;
        }

        private ObservableCollection<ParamDetail> Convert2ParamDetail(ObservableCollection<VersionContrast> versionConetastCollection)
        {
            ParamDetail paramDetail = null;
            ObservableCollection<ParamDetail> paramDetailCollection = new ObservableCollection<ParamDetail>();
            foreach (VersionContrast item in versionConetastCollection)
            {
                paramDetail = new ParamDetail();
                paramDetail.ParameterDetail = (string)ParamDetailTable[item.versionContrastPk.parameterId];
                paramDetail.ParameterID = item.versionContrastPk.parameterId;
                paramDetail.ParameterNum = item.parameterNum;
                paramDetail.VersionNum = item.versionContrastPk.versionNum;
                paramDetailCollection.Add(paramDetail);
            }
            return paramDetailCollection;
        }

        void ParaCollectionDetail_CurrentChanged(object sender, System.EventArgs ep)
        {
            if (ParaCollectionDetail.CurrentItem != null)
            {
                ParamDetail paramDetail = ParaCollectionDetail.CurrentItem as ParamDetail;
                this.ParamInfo = "参数" + paramDetail.ParameterID + " 大版本:" + paramDetail.VersionNum + " 参数版本:" + paramDetail.ParameterNum;
                //查询inner_ctrl表中的启用时间和结束时间
                InnerVerCtrl innerVerCtrl = innerVerCtrlDAO.GetInnerVerCtrlByParamIDAndParamNum(paramDetail.ParameterID, paramDetail.ParameterNum);
                if (innerVerCtrl != null)
                {
                    this.StartTime = Convert.ToDateTime(ConvertDateFormat(innerVerCtrl.startTime));
                    this.EndTime = Convert.ToDateTime(ConvertDateFormat(innerVerCtrl.endTime));
                }
                else
                {
                    //this.StartTime = DateTime.Now;
                    this.StartTime = Convert.ToDateTime(ConvertDateFormat("19000101000000"));
                    this.EndTime = Convert.ToDateTime(ConvertDateFormat("20991231235959"));
                    Microsoft.Windows.Controls.MessageBox.Show("系统内部参数版本控制表中无此参数或参数版本，请为其设置启用时间和结束时间");
                }
            }
        }

        private string ConvertDateFormat(string sFormat)
        {
            string tmp = "";
            int offset = 0;
            tmp += sFormat.Substring(offset, 4);
            offset += 4;
            tmp += "-";
            tmp += sFormat.Substring(offset, 2);
            offset += 2;
            tmp += "-";
            tmp += sFormat.Substring(offset, 2);
            offset += 2;
            tmp += " ";
            tmp += sFormat.Substring(offset, 2);
            offset += 2;
            tmp += ":";
            tmp += sFormat.Substring(offset, 2);
            offset += 2;
            tmp += ":";
            tmp += sFormat.Substring(offset, 2);
            return tmp;
        }

        class ParamDetail
        {
            public string ParameterID { get; set; }
            public string ParameterDetail { get; set; }
            public int VersionNum { get; set; }
            public int ParameterNum { get; set; }
        }

    }
}
